// pages/aduting/resversion/resersion_list.js

var util = require('../../../utils/util.js');
var app = getApp();
var limit = 15;// 每页显示的条数
var today = util.formatTime(new Date());
var minday = util.formatTime(new Date());
var maxday = util.formatTime(new Date());
var listArray=[];//公寓list
var roomArray=[];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    apartment_id:'',
    apartment_name:'',
    reservation_state:'',
    keyword:'',
    beginDate:'',
    endDate:'',
    date: today,
    minday: today,
    maxday: maxday,
    apartmentArray:'',
    room_name:'',
    roomArray:'',
    room_id: '-1',
    stateArray: [{ "key": "待处理", "value": '0' }, { "key": "已同意", "value": '1' }, { "key": "已拒绝", "value": '2' }, { "key": "已过期", "value": '3' }],
    stateName:'',
    url:'/pages/aduting/resversion/reversion_detail',
    nomore:false,
    hiddenmodalput: true,
    time1: '开始日期',
    time2: '结束日期',
    apartment_name1: '楼栋',
    room_name1: '房间号',
    stateName1: '审批状态',
    scrollTop: 0,//设置srcoll-view距离顶部位置
    scrollHeight: 0,
    dataList: [],
    currentPage: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // 上拉加载更多
  loadMore: function (e) {
    var that = this;
    if (!that.data.nomore) {
      that.setData({
        currentPage: that.data.currentPage + 1,
      })
      util.loading();
      that.getDataofres(that.data.userInfo);
    }
  },
  /**
* 滑动事件
* */
  scroll: function (e) {
    var that = this;
    that.setData({
      scrollTop: e.detail.scrollTop
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight - 20
        });
      }
    });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        that.setData({
          userInfo: res.data
        })

        that.getDataofres(res.data);
        that.getApartment(res.data);
        that.getDataofRoom(res.data);
      }, fail: function () {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },
  getDataofres:function(data){
    var that=this;
    util.getReq("/app/my/reservationList", { source: data.source,
     uid: data.user_id,
     'term.pageIndex': that.data.currentPage,
     "term.limit": limit,
     'term.apartment_id': that.data.apartment_id,
     'term.room_id': that.data.room_id,
     'term.reservation_state': that.data.reservation_state,
     'term.keyword': that.data.keyword,
     'term.beginDate': that.data.beginDate,
     'term.endDate': that.data.endDate
      }, function (datas) {
        var dataLists = that.data.dataList;
        util.closeloading();
        wx.stopPullDownRefresh();
      if (datas.code == 0) {
        if (datas.data.data) {
          let len = datas.data.data.length;
          for (var i = 0; i < len; i++) {
           
            dataLists.push(datas.data.data[i]);
          }
          that.setData({
            dataList: dataLists
          })
          if (datas.data.pageCount<=that.data.currentPage) {
            that.setData({
              nomore: true
            })
          }
        }else{
          that.setData({
            nomore: true
          })
        }
        
      } else {
        wx.showToast({
          title: datas.desc,
        })
       
      }
    });

  },

 
 //选择开始日期
 bindDateStartChange: function (e) {
   this.setData({
     beginDate: e.detail.value,
     time1: e.detail.value
   })
 },
 //选择结束日期
 bindDateEndChange: function (e) {
   this.setData({
     endDate: e.detail.value,
     time2: e.detail.value
   })
 },
 //选择公寓
 bindPartmentChange:function(e){
   var str="";
   if (this.data.apartmentArray[e.detail.value].key.length>5){ 
     str = this.data.apartmentArray[e.detail.value].key.substring(0,5)+"...";
   }else{
     str = this.data.apartmentArray[e.detail.value].key;
   }
   this.setData({
     apartment_id: this.data.apartmentArray[e.detail.value].value,
     apartment_name: str,
     apartment_name1: str,
     room_name: '',
     roomArray: '',
     room_id: '-1',
     room_name1: '房间号',
   })
   this.getDataofRoom(this.data.userInfo);
 },
 //获取公寓
 getApartment:function(data){
  var that=this;
  util.getReq("/app/home/allApartmentInterface", {
     source: data.source,
     uid: data.user_id
   }, function (datas) {
   
     if (datas.code == 0) {
      var list=datas.params.list;
      var lth = list.length;
      listArray=[];
      if(lth>0){
          for(var i=0;i<lth;i++){
            var ob=new Object();
            ob.key = list[i].apartment_name;
            ob.value = list[i].apartment_id;
            listArray.push(ob);

          }

      }
       that.setData({
         apartmentArray: listArray

       })
     } else {
       wx.showToast({
         title: datas.desc,
       })
     }
     
   })
 },
 //获取房间
 getDataofRoom:function(data){
   var that = this;

   util.getReq("/app/home/allRoomInterface", {
     source: data.source,
     uid: data.user_id,
     apartment_id:that.data.apartment_id
   }, function (datas) {
     if (datas.code == 0) {
       var list = datas.params.list;
       var lth = list.length;
       roomArray=new Array();
       if (lth > 0) {
         for (var i = 0; i < lth; i++) {
           roomArray.push({ "key": list[i].room_code,"value":list[i].room_id});
         }

       }
       that.setData({
         roomArray: roomArray
       })
     } else {
       wx.showToast({
         title: datas.desc,
       })
     }

   })
 },
 //选择房间
 bindRoomChange:function(e){
   this.setData({
     room_id: this.data.roomArray[e.detail.value].value,
     room_name: this.data.roomArray[e.detail.value].key,
     room_name1: this.data.roomArray[e.detail.value].key
   })

 },
 //状态选择
 bindStateChange:function(e){
   this.setData({
     stateName: this.data.stateArray[e.detail.value].key,
     reservation_state: this.data.stateArray[e.detail.value].value,
     stateName1: this.data.stateArray[e.detail.value].key
   })

 },
 ///取消时不显示
 termCel: function (e) {
   this.setData({
     hiddenmodalput: true,
     apartment_id: '',
     apartment_name: '',
     room_id: '-1',
     check_state: '',
     keyword: '',
     beginDate: '',
     endDate: '',
     time1: '开始日期',
     time2: '结束日期',
     apartment_name1: '请选择楼栋',
     room_name1: '请选择房间号',
     stateName1: '请选择审批状态',
   })

 },
 //更多打开
 moreTap: function (e) {
   this.setData({
     hiddenmodalput: false

   })
 },
 //确定
 termConfirm: function (e) {
   this.setData({
     hiddenmodalput: true,
   })
   this.bindSearch(e);
 },
  
  bindSearch:function(e){
    this.setData({
      nomore: false,
      dataList:[],
      currentPage: 1,
    })

    this.getDataofres(this.data.userInfo);
  },

 //输入框
 wxSearchInput: function (e) {

   this.setData({

     "wxSearchData.value": e.detail.value,
     keyword: e.detail.value
   })
 }

})